//
//  HYBookSuccessView.m
//  Booking
//
//  Created by Duntech on 2018/5/14.
//  Copyright © 2018年 Duntech. All rights reserved.
//

#import "HYBookSuccessView.h"

@interface HYBookSuccessView ()

@property (nonatomic,strong) UIView *whiteBgView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIImageView *iconImgView;
@property (nonatomic,strong) UIButton *confirmBtn;

@end


@implementation HYBookSuccessView

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        [self addSubview:self.whiteBgView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.iconImgView];
        [self addSubview:self.confirmBtn];
    }
    return self;
}

- (instancetype)initwithTitle:(NSString *)title{
    
    if (self == [super init]) {
        
        [self addSubview:self.whiteBgView];
        [self addSubview:self.titleLabel];
        [self addSubview:self.iconImgView];
        [self addSubview:self.confirmBtn];
        self.titleLabel.text = title;
    }
    return self;
}

- (void)layoutSubviews{
    
    [_whiteBgView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.equalTo(self).offset(70 * WIDTH_MULTIPLE);
        make.right.equalTo(self).offset(-70 * WIDTH_MULTIPLE);
        make.top.equalTo(self).offset(160 * WIDTH_MULTIPLE);
        make.bottom.equalTo(self).offset(-160 * WIDTH_MULTIPLE);
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.whiteBgView).offset(10 * WIDTH_MULTIPLE);
        make.right.equalTo(self.whiteBgView).offset(-10 * WIDTH_MULTIPLE);
        make.top.equalTo(self.whiteBgView).offset(20 * WIDTH_MULTIPLE);
        make.height.mas_equalTo(60 * WIDTH_MULTIPLE);
    }];
    
    [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.titleLabel.mas_bottom).offset(40 * WIDTH_MULTIPLE);
        make.centerX.equalTo(self.whiteBgView);
        make.height.width.mas_equalTo(60 * WIDTH_MULTIPLE);
    }];
    
    [_confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.whiteBgView);
        make.bottom.right.equalTo(self.whiteBgView).offset(-20 * WIDTH_MULTIPLE);
        make.left.equalTo(self.whiteBgView).offset(20 * WIDTH_MULTIPLE);
    }];
}

#pragma mark - publicMethod
- (void)showBookSuccessView{
    
    self.frame = KEYWINDOW.bounds;
    [KEYWINDOW addSubview:self];
    
    [UIView animateWithDuration:0.2 animations:^{
       
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.66];
    }];
}

-(void)setTitleText:(NSString *)title{
    self.titleLabel.text = title;
}

#pragma mark - lazyload
- (UIView *)whiteBgView{
    
    if (!_whiteBgView) {
        _whiteBgView = [UIView new];
        _whiteBgView.layer.cornerRadius = 8 * WIDTH_MULTIPLE;
        _whiteBgView.backgroundColor = KAPP_WHITE_COLOR;
    }
    return _whiteBgView;
}

- (UILabel *)titleLabel{
    
    if (!_titleLabel) {
        
        _titleLabel = [UILabel new];
        _titleLabel.numberOfLines = 0;
        _titleLabel.textColor = KAPP_BLACK_COLOR;
        _titleLabel.font = KFitFont(18);
        _titleLabel.text = @"Room booked\nsuccessfully";
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIImageView *)iconImgView{
    
    if (!_iconImgView) {
        
        _iconImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"book_success"]];
    }
    return _iconImgView;
}

- (UIButton *)confirmBtn{
    
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _confirmBtn.layer.cornerRadius = 10 * WIDTH_MULTIPLE;
        _confirmBtn.backgroundColor = KAPP_THEME_COLOR;
        [_confirmBtn setTitle:NSLocalizedString(@"ok", nil) forState:UIControlStateNormal];
        [_confirmBtn setTitleColor:KAPP_WHITE_COLOR forState:UIControlStateNormal];
        __weak typeof(self) weakSelf = self;
        [_confirmBtn addBlockForControlEvents:UIControlEventTouchUpInside block:^(id  _Nonnull sender) {
            if (weakSelf.bookSuccessBlock) {
                
                weakSelf.bookSuccessBlock();
                [weakSelf removeFromSuperview];
            }
        }];
    }
    return _confirmBtn;
}

@end
